package com.duoduo.springboot.shiro.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.duoduo.springboot.shiro.model.Role;

@Repository
public class RoleDao {

	private static Map<Integer, Role> roleDatas = new HashMap<Integer, Role>(0);
	private static Map<Integer, List<Integer>> userRoleMap = new HashMap<Integer, List<Integer>>(0);

	private Map<Integer, Role> listRoles() {
		if (CollectionUtils.isEmpty(roleDatas)) {
			roleDatas.put(1, new Role(1, "admin"));
			roleDatas.put(2, new Role(2, "user"));
		}
		return roleDatas;
	}

	private Map<Integer, List<Integer>> listUserRole() {
		if (CollectionUtils.isEmpty(userRoleMap)) {
			List<Integer> roles = new ArrayList<Integer>(0);
			roles.add(1);
			userRoleMap.put(1, roles);

			roles = new ArrayList<Integer>(0);
			roles.add(2);
			userRoleMap.put(2, roles);
		}
		return userRoleMap;
	}

	public List<Role> listRolesByUserId(int userId) {
		List<Role> result = new ArrayList<Role>(0);
		if (listUserRole().containsKey(userId)) {
			List<Integer> ids = userRoleMap.get(userId);
			for (Integer id : ids) {
				result.add(listRoles().get(id));
			}
		}
		return result;
	}
}
